package com.chaoxuan.uebt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.activity.LoginActivity;
import com.chaoxuan.uebt.adapter.TimelineAdapter;
import com.chaoxuan.uebt.model.Index;
import com.chaoxuan.uebt.model.Timeline;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.google.common.base.Splitter;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/3/13.
 */
public class WelcomeFragment extends Fragment implements View.OnClickListener {
    RecyclerView recyclerView;
    TimelineAdapter adapter;
    TextView tvDesc;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_welcome, container, false);
        tvDesc=(TextView) rootView.findViewById(R.id.tv_desc);
        rootView.findViewById(R.id.btn_dig).setOnClickListener(this);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView=(RecyclerView) rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter= new TimelineAdapter();
        recyclerView.setAdapter(adapter);
        init();
        return rootView;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_dig:
                startActivity(new Intent(getContext(),LoginActivity.class));
                break;
        }
    }

    private void init(){
        RetrofitUtils.getAPIService().index("token",Const.DEVICE_TYPE)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseSubscriber<Index>(){
                @Override
                public void onSuccess(Index d) {
                    tvDesc.setText(d.getDesc());
                    List<Timeline> data=new ArrayList<Timeline>();
                    boolean left=false;
                    int cur=1;
                    String str=d.getMap();//"2018年Q4 项目启动 2019年Q1 主网内测版上线 2019年Q2 挖矿算力上线 2019年Q3/Q4 登陆交易所 2020年Q1/Q2 积分联盟上线 2020年Q3/Q4 算力生态上线";
                    List<String> list=Splitter.on(" 2").splitToList(str);
                    for(int i=0;i<list.size();i++) {
                        String s=list.get(i);
                        List<String> tmp=Splitter.on(" ").splitToList(s);
                        String text=tmp.get(1);
                        String time=tmp.get(0);
                        if(time.startsWith("0"))time="2"+time;
                        if(i%2==0) {
                            left=!left;
                            data.add(new Timeline(i==0,i<cur,left, time,text));
                        }else {
                            left=!left;
                            data.add(new Timeline(i==0,i<cur,left, time,text));
                        }
                    }

                    adapter.addData(data);
                }
            });
    }

}
